package com.ha.controller.user;

import com.ha.constant.JwtClaimsConstant;
import com.ha.dto.UserLoginDTO;
import com.ha.dto.UserRegisterDTO;
import com.ha.entity.User;
import com.ha.properties.JwtProperties;
import com.ha.result.Result;
import com.ha.service.UserService;
import com.ha.utils.JwtUtil;
import com.ha.vo.EmployeeLoginVO;
import com.ha.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * ClassName: Usercontroller
 * Package: com.ha.controller.user
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/4/22  20:52
 * @Version 1.0
 */
@RestController("UserUserController")
@RequestMapping("/user/user")
@Api(tags = "用户接口")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {

        log.info("用户登录{}", userLoginDTO);
        //校验是否有该身份
        User user = userService.login(userLoginDTO);

        //登录成功生成jwt令牌
        HashMap<String, Object> claim = new HashMap<>();
        claim.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claim
        );
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .name(user.getName())
                .token(token)
                .userName(user.getUsername())
                .build();

        return Result.success(userLoginVO);
    }

    @GetMapping("/test1")
    public  Result<String> test(){
        log.info("test1被请求");
        return Result.success("123");
    }
    /**
     * 更新用户
     * @param userRegisterDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("更新用户")
    public Result updaye(@RequestBody UserRegisterDTO userRegisterDTO){
        log.info("更新用户{}",userRegisterDTO);
        userService.updateUser(userRegisterDTO);
        return Result.success();
    }

}
